package com.vercon.commonmirror.view

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.ProgressBar

import com.vercon.commonmirror.R
import com.vercon.commonmirror.utils.DensityUtils

/**
 * Created by YZC on 2019/9/3
 */
class AssessProgress : LinearLayout {

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        bindView()
    }

    lateinit var tv_name: NormalTextView
    lateinit var tv_value: NormalTextView
    // lateinit var pb_percent: ProgressBar
    lateinit var ll_assess: LinearLayout
    var mProgressBarList: ArrayList<ProgressBar>? = null
    var size:Int = 0

    fun bindView() {
        LayoutInflater.from(context).inflate(R.layout.widget_smarthome_assess_progress, this)
        mProgressBarList = ArrayList()
        tv_name = findViewById(R.id.tv_name)
        tv_value = findViewById(R.id.tv_value)
        mProgressBarList!!.add(findViewById(R.id.progressBar1))
        mProgressBarList!!.add(findViewById(R.id.progressBar2))
        mProgressBarList!!.add(findViewById(R.id.progressBar3))
        mProgressBarList!!.add(findViewById(R.id.progressBar4))
        // pb_percent = findViewById(R.id.pb_percent)
        ll_assess = findViewById(R.id.ll_assess)

    }

    var texts = arrayListOf<NormalTextView>()

    fun init(max: Int, name: String, assess: ArrayList<String>) {
        //pb_percent.max = max
        size = assess.size
        tv_name.text = name

        val res = context.resources
        val lineWidth = DensityUtils.dip2px(context, 2f)
        val paddingTop = DensityUtils.dip2px(context, res.getDimension(R.dimen.dp_5))
        assess.forEach {
            var line = View(context).apply {
                setBackgroundColor(Color.parseColor("#33FFFFFF"))
                layoutParams = LayoutParams(lineWidth, lineWidth * 4, 0f)
            }
            ll_assess.addView(line)

            var text = NormalTextView(context).apply {
                gravity = Gravity.CENTER
                text = it
                textSize = res.getDimension(R.dimen.sp_6)
                setTextColor(res.getColor(R.color.hint))
                layoutParams = LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f)
                setPadding(0, paddingTop, 0, 0)
            }
            texts.add(text)
            ll_assess.addView(text)
        }

        var line = View(context).apply {
            setBackgroundColor(Color.parseColor("#33FFFFFF"))
            layoutParams = LayoutParams(lineWidth, lineWidth * 4, 0f)
        }
        ll_assess.addView(line)
    }

    fun setData(value: Int, max: Int = 30){
        //  pb_percent.max = max
        tv_value.text = value.toString()
        // pb_percent.progress = value
    }
    fun setData(value: Int,IntervalValue:Int,max:Int,progress:Int,progressMax:Int,assess: ArrayList<String>){

        val num = IntervalValue.toDouble() / max
        val num2 = assess.size * num
        var integer = (num2 / 1).toInt()
        val remainder = num2 % 1
        if (remainder > 0) {
            integer += 1
        }
        //  pb_percent.max = max
        tv_value.text = value.toString()
        // pb_percent.progress = value
        for ( i in assess.indices){
            //进度条设置
            mProgressBarList?.get(i)?.visibility = View.VISIBLE
            mProgressBarList?.get(i)?.max = progressMax

            if (i < integer - 1) {
                mProgressBarList?.get(i)?.progress = progressMax
            } else if (i == integer - 1) {
                mProgressBarList?.get(i)?.progress = progress
            } else {
                mProgressBarList?.get(i)?.progress = 0
            }
        }
    }
}